package by.yakusekmaxim.bookgid.services.impl;

import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.yakusekmaxim.bookgid.dataaccess.ReviewDao;
import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.Review;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.services.ReviewService;

@Service
public class ReviewServiceImpl implements ReviewService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReviewServiceImpl.class);

    @Inject
    private ReviewDao dao;

    @Override
    public Review get(Long id) {
    	Review entity = dao.getById(id);
        return entity;
    }

    @Override
    public void saveOrUpdate(Review review) {
        if (review.getId() == null) {
            LOGGER.debug("Save new: {}", review);
            dao.insert(review);
        } else {
            LOGGER.debug("Update: {}", review);
            dao.update(review);
        }
    }

    @Override
    public void delete(Review review) {
        LOGGER.debug("Remove: {}", review);
        dao.delete(review.getId());

    }

    @Override
    public void deleteAll() {
        LOGGER.debug("Remove all reviews");
        dao.deleteAll();
    }

	@Override
	public List<Review> getAllReview(Book book, boolean ascending) {
		return dao.getAllReview(book, ascending);
	}

	@Override
	public Review getReview(Book book, UserAccount user) {
		List<Review> list = dao.getReview(book, user);
		Review review = null;
		if (list != null & list.size()!=0) {
			review = list.get(0);
		}
		return review;
	}

}
